package com.dream.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dream.bean.Employee;
import com.dream.bean.EmployeeExample;
import com.dream.bean.EmployeeExample.Criteria;
import com.dream.dao.EmployeeMapper;

@Service
public class EmployeeService {

	@Autowired
	private EmployeeMapper employeeMapper;
	
	/**
	 * 查询所有员工和员工的部门信息
	 * @return
	 */
	public List<Employee> getEmployeeAll() {
		EmployeeExample employeeExample=new EmployeeExample();
		employeeExample.setOrderByClause("emp_id asc");
		List<Employee> list = employeeMapper.selectByExampleWithDept(employeeExample);
		return list;
	}

	/**
	 * 添加员工
	 * @param employee 员工对象
	 * @return 
	 */
	public int employeeAdd(Employee employee) {
		int resulte = employeeMapper.insertSelective(employee);
		return resulte;
	}

	/**
	 * 检查该姓名员工是否存在
	 * @param employeeName
	 * @return 是否存在
	 */
	public boolean checkEmployeeName(String employeeName) {
		EmployeeExample employeeExample = new EmployeeExample();
		Criteria criteria = employeeExample.createCriteria();
		criteria.andEmpNameEqualTo(employeeName);
		long result = employeeMapper.countByExample(employeeExample);
		return result==0;
	}

	/**
	 * 删除员工
	 * @param ids 需要删除的员工Id集合
	 * @return 是否删除成功
	 */
	public boolean employeeDel(List<Integer> ids) {
		EmployeeExample employeeExample = new EmployeeExample();
		Criteria criteria = employeeExample.createCriteria();
		criteria.andEmpIdIn(ids);
		int result = employeeMapper.deleteByExample(employeeExample);
		return result>0?true:false;
	}

	/**
	 * 得到员工对象ById
	 * @param id
	 * @return
	 */
	public Employee getEmployee(Integer id) {
		EmployeeExample employeeExample = new EmployeeExample();
		Criteria criteria = employeeExample.createCriteria();
		criteria.andEmpIdEqualTo(id);
		List<Employee> list = employeeMapper.selectByExampleWithDept(employeeExample);
		if (list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 更新员工信息
	 * @param employee
	 * @return 是否更新成功
	 */
	public boolean employeeUpdate(Employee employee) {
		EmployeeExample employeeExample = new EmployeeExample();
		Criteria criteria = employeeExample.createCriteria();
		criteria.andEmpIdEqualTo(employee.getEmpId());
		employee.setEmpId(null);
		int result = employeeMapper.updateByExampleSelective(employee, employeeExample);
		return result>0?true:false;
	}

	
}
